package com.huluobo.h3kotlinproject.mine

import android.content.Intent
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.SPUtils
import com.huluobo.h3kotlinproject.R
import com.huluobo.h3kotlinproject.base.BaseMVVMFragment
import com.huluobo.h3kotlinproject.databinding.FragmentMineBinding
import com.huluobo.h3kotlinproject.login.LoginActivity
import com.huluobo.h3kotlinproject.net.Const
import com.huluobo.h3kotlinproject.util.ImageLoader


class MineFragment : BaseMVVMFragment<FragmentMineBinding>() {
    private lateinit var viewModel: MineViewModel
    override fun getLayoutId(): Int = R.layout.fragment_mine

    override fun initData() {
        viewModel = ViewModelProvider(this)[MineViewModel::class.java]

        viewModel.queryUserInfo(SPUtils.getInstance().getInt(Const.SP_USER_ID))

        viewModel.successUserInfo.observe(this) {
            dataBinding.mineTvNickName.text = it.nickname
            ImageLoader.loadCircleImage(requireActivity(), it.icon, dataBinding.mineIvUserIcon)
        }
    }

    override fun initView() {
        dataBinding.mineTvLogout.setOnClickListener {
            SPUtils.getInstance().remove(Const.SP_TOKEN)
            SPUtils.getInstance().remove(Const.SP_USER_ID)
            val intent = Intent(requireActivity(), LoginActivity::class.java)
            startActivity(intent)
            requireActivity().finish()
        }
    }
}